﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace PlaceHR.Processor.Processor.DailyEvent.Steps
{
    class VisaExpiredStepCalculator : StepCalculatorBase<DailyEventProcessorContext, IList<EmployeeAlert>>
    {
        public override void Calculate(DailyEventProcessorContext dailyEventProcessorContext, IList<EmployeeAlert> employeeAlertList)
        {
            DateTime now = DateTime.Now;

            int alertBeforeDays = 7;
            if (ConfigurationManager.AppSettings["AlertBeforeDays"] != null)
            {
                alertBeforeDays = Convert.ToInt32(ConfigurationManager.AppSettings["AlertBeforeDays"]);
            }

            // Residence Expire
            if (dailyEventProcessorContext.CurrentEmployee.ResidenceExpireDate.HasValue && dailyEventProcessorContext.CurrentEmployee.ResidenceExpireDate.Value >= now)
            {
                this.TryCreateAlert(dailyEventProcessorContext, AlertType.ResidenceExpired, dailyEventProcessorContext.CurrentEmployee.ResidenceExpireDate.Value, now, alertBeforeDays);
            }

            // Visa Expire
            if (dailyEventProcessorContext.CurrentEmployee.VisaExpireDate.HasValue && dailyEventProcessorContext.CurrentEmployee.VisaExpireDate.Value >= now)
            {
                this.TryCreateAlert(dailyEventProcessorContext, AlertType.VisaExpired, dailyEventProcessorContext.CurrentEmployee.VisaExpireDate.Value, now, alertBeforeDays);
            }

            // Work Permit Expire
            if (dailyEventProcessorContext.CurrentEmployee.WorkPermitExpireDate.HasValue && dailyEventProcessorContext.CurrentEmployee.WorkPermitExpireDate.Value >= now)
            {
                this.TryCreateAlert(dailyEventProcessorContext, AlertType.WorkPermitExpired, dailyEventProcessorContext.CurrentEmployee.WorkPermitExpireDate.Value, now, alertBeforeDays);
            }
        }

        private void TryCreateAlert(DailyEventProcessorContext dailyEventProcessorContext
            , string alertType
            , DateTime expireDate
            , DateTime now
            , int alertBeforeDays)
        {
            var spanDays = (expireDate - now).TotalDays;

            if (spanDays <= alertBeforeDays)
            {
                bool existingAlert = dailyEventProcessorContext.CurrentEmployee.EmployeeAlert.Any(p => p.ExpireDate == expireDate
                        && p.AlertTypeCode.Equals(alertType, StringComparison.Ordinal));

                if (!existingAlert)
                {
                    EmployeeAlert alert = new EmployeeAlert
                    {
                        EmployeeID = dailyEventProcessorContext.CurrentEmployee.EmployeeID,
                        AlertTypeCode = alertType,
                        IsResolved = false,
                        ExpireDate = expireDate,
                        UpdateDate = DateTime.Now,
                        UpdateBy = dailyEventProcessorContext.CurrentEmployee.EmployeeID
                    };

                    dailyEventProcessorContext.CurrentEmployee.EmployeeAlert.Add(alert);
                }
            }
        }

    }
}
